package com.team8.search.service;

import com.team8.search.pojo.FlightInfoES;
import com.team8.search.repository.FlightInfoRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;

@Service
public class FlightInfoService {

    private final ThreadLocal<SimpleDateFormat> simpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyyMMddHHmmss");
        }
    };

    @Autowired
    private FlightInfoRepository flightInfoRepository;

    public Page<FlightInfoES> queryFlightByPort(String start, String end, String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = this.simpleDateFormat.get();

        Long dayBegin = simpleDateFormat.parse(date + "000000").getTime();
        Long dayEnd = dayBegin + 86400000;

        System.out.println(dayBegin);
        System.out.println(dayEnd);

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
//        mustQuery.must(QueryBuilders.matchAllQuery()); // 添加第1条must的条件 此处为匹配所有文档
        mustQuery.must(QueryBuilders.matchQuery("start", start));
        mustQuery.must(QueryBuilders.matchQuery("end", end));
        mustQuery.must(QueryBuilders.rangeQuery("startTime").gte(dayBegin).lt(dayEnd));

        queryBuilder.withQuery(mustQuery);
        Page<FlightInfoES> search = flightInfoRepository.search(queryBuilder.build());
        Iterator<FlightInfoES> iterator = search.iterator();
        while(iterator.hasNext()){
            FlightInfoES next = iterator.next();

            System.out.println(next);
        }
        return search;
    }

    public Page<FlightInfoES> queryFlightByFfid(String ffid, String date) throws Exception {
        SimpleDateFormat simpleDateFormat = this.simpleDateFormat.get();

        Long dayBegin = simpleDateFormat.parse(date + "000000").getTime();
        Long dayEnd = dayBegin + 86400000;

        System.out.println(dayBegin);
        System.out.println(dayEnd);

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
//      mustQuery.must(QueryBuilders.matchAllQuery()); // 添加第1条must的条件 此处为匹配所有文档
        mustQuery.must(QueryBuilders.matchQuery("ffid", ffid));
        mustQuery.must(QueryBuilders.rangeQuery("startTime").gte(dayBegin).lt(dayEnd));

        queryBuilder.withQuery(mustQuery);
        Page<FlightInfoES> search = flightInfoRepository.search(queryBuilder.build());
        Iterator<FlightInfoES> iterator = search.iterator();
        while(iterator.hasNext()){
            FlightInfoES next = iterator.next();

            System.out.println(next);
        }
        return search;
    }
}
